package com.sc.nft.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.enums.DivideTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.UserDaoPerformanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 用户DAO业绩服务接口实现
 *
 * @author zxy
 * @since 2023-12-12 17:26:18
 * @description 商户banner
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class UserDaoPerformanceServiceImpl implements UserDaoPerformanceService {
    private final UserDaoPerformanceDao userDaoPerformanceDao;
    private final OrderDao orderDao;
    private final UserInfoDao userInfoDao;
    private final ShareProfitDao shareProfitDao;
    private final Long META_DAO_ID= 6L;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calFirstOrder(Order order, String recordDate, Map<Long, Integer> termUser) {
        UserInfo userInfo = userInfoDao.getById(order.getUserId());
        List<ShareProfit> shareProfits = shareProfitDao.getByPanelIdAndType(order.getShareProfitPanelId(), DivideTypeEnum.DAO);
        boolean settleCenter = false;
        for (int i = 0; i <= 99; i++) {

            for (ShareProfit shareProfit : shareProfits) {
                UserDaoPerformance userDaoPerformance = get(shareProfit.getDaoId(), userInfo.getId(), recordDate);
                userDaoPerformance.addFirstPerformance(order.getProductPrice().multiply(shareProfit.getSumRatio()));
                userDaoPerformance.insertOrUpdate();
            }
            if(!settleCenter){
                if(MapUtil.getInt(termUser,userInfo.getId(),0).intValue()==2){
                    settleCenter = true;
                    UserDaoPerformance userDaoPerformance = get(META_DAO_ID, userInfo.getId(), recordDate);
                    userDaoPerformance.addFirstCenterPerformance(order.getProductPrice());
                    userDaoPerformance.insertOrUpdate();
                }
            }
            if(userInfo.getRecommendUserId()==null||userInfo.getRecommendUserId().longValue()==1L){
                return;
            }
            userInfo =  userInfoDao.getById(userInfo.getRecommendUserId());
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void calSecondOrder(SecondOrder order, String recordDate, Map<Long, Integer> termUser) {
        UserInfo userInfo = userInfoDao.getById(order.getBuyUserId());
        List<ShareProfit> shareProfits = shareProfitDao.getByPanelIdAndType(order.getShareProfitPanelId(), DivideTypeEnum.DAO);
        boolean settleCenter = false;
        for (int i = 0; i <= 99; i++) {

            for (ShareProfit shareProfit : shareProfits) {
                UserDaoPerformance userDaoPerformance = get(shareProfit.getDaoId(), userInfo.getId(), recordDate);
                userDaoPerformance.addSecondPerformance(order.getBuyPrice().multiply(shareProfit.getSumRatio()));
                userDaoPerformance.insertOrUpdate();
            }
            if(!settleCenter){
                if(MapUtil.getInt(termUser,userInfo.getId(),0).intValue()==2){
                    settleCenter = true;
                    UserDaoPerformance userDaoPerformance = get(META_DAO_ID, userInfo.getId(), recordDate);
                    userDaoPerformance.addSecondCenterPerformance(order.getBuyPrice());
                    userDaoPerformance.insertOrUpdate();
                }
            }
            if(userInfo.getRecommendUserId()==null||userInfo.getRecommendUserId().longValue()==1L){
                return;
            }
            userInfo =  userInfoDao.getById(userInfo.getRecommendUserId());
        }
    }



    private UserDaoPerformance get(Long daoId,Long userId,String recordDate){
        UserDaoPerformance userDaoPerformance = userDaoPerformanceDao.getByDaoIdAndUserId(daoId, userId, recordDate);
        if(ObjectUtil.isNull(userDaoPerformance)){
            userDaoPerformance = new UserDaoPerformance();
            userDaoPerformance.setDaoId(daoId);
            userDaoPerformance.setRecordDate(recordDate);
            userDaoPerformance.setUserId(userId);
            userDaoPerformance.setFirstPerformance(BigDecimal.ZERO);
            userDaoPerformance.setSecondPerformance(BigDecimal.ZERO);
            userDaoPerformance.setFirstCenterPerformance(BigDecimal.ZERO);
            userDaoPerformance.setSecondCenterPerformance(BigDecimal.ZERO);
        }
        return userDaoPerformance;


    }
}